//
//  MainHeaderCollectionCell.m
//  Stream
//
//  Created by shaw on 16/8/13.
//  Copyright © 2016年 showshaw. All rights reserved.
//

#import "MainHeaderCollectionCell.h"


//设置图片个数
#define zImageCount 3
//设置宽度
#define zScrollViewWidth ([UIScreen mainScreen].bounds.size.width)

@interface MainHeaderCollectionCell()<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *scrollContentView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
//定时器
@property (weak,nonatomic)NSTimer *timer;
@end


@implementation MainHeaderCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];

    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapFunc:)];
    [self.scrollView addGestureRecognizer:gesture];
    
    self.layer.cornerRadius = 5.0f;
    self.layer.masksToBounds = YES;
    
    self.scrollView.userInteractionEnabled = YES;
    
    //加载轮播图片
    [self loadScrollView];
    //加载页码
    [self loadPageControl];
    //加载定时器
    [self loadTimer];
    
    
}

-(void)tapFunc:(UIGestureRecognizer*)gesture
{
    NSLog(@"1");
}
//加载轮播图片
- (void)loadScrollView{
    
    for (int i = 0; i < zImageCount; i++) {
        //计算imageView的X的坐标
        CGFloat imageViewX = i * zScrollViewWidth;

        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(imageViewX, 0, zScrollViewWidth, 160.0f)];
        //设置我们的图片
        NSString *imageName = @"weather_sunny_bg";

        //设置图片名称
        imageView.image = [UIImage imageNamed:imageName];
        //添加到轮播控制器中
        [self.scrollContentView addSubview:imageView];
    }
    //计算imageView的宽度
    CGFloat imageViewW = zImageCount * zScrollViewWidth;
    
    //给轮播器设置滚动范围
    self.scrollView.contentSize = CGSizeMake(imageViewW, 160.0f);
    //隐藏滚动条
    self.scrollView.showsHorizontalScrollIndicator = YES;
    //设置分页
    self.scrollView.pagingEnabled=YES;
    //设置代理
    self.scrollView.delegate = self;
    
    self.scrollView.scrollEnabled = YES;
    
}

//加载页码
- (void)loadPageControl{
    
    [self.scrollView bringSubviewToFront:self.pageControl];
    
    //设置页面总个数
    self.pageControl.numberOfPages = zImageCount;
    //设置当前页码
    self.pageControl.currentPage = 0;
    //设置当前页码的颜色
    self.pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
    //设置其他页码的颜色
    self.pageControl.pageIndicatorTintColor = [UIColor grayColor];
    
}

//加载定时器
- (void)loadTimer{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(pageChanged:) userInfo:nil repeats:YES];
}

//当页码发生改变的时候调用
- (void)pageChanged:(id)sender{
    
    //获取当前页面的索引
    NSInteger currentPage=self.pageControl.currentPage;
    //获取偏移量
    CGPoint offset=self.scrollView.contentOffset;
    //
    if (currentPage >= zImageCount - 1) {
        //将其设置首张图片的索引
        currentPage=0;
        //恢复偏移量
        offset.x = 0;

    }else{
        //当前索引+1
        currentPage ++;
        //设置偏移量
    
        offset.x += zScrollViewWidth;
    }

    [UIView animateWithDuration:0.5 animations:^{
        self.scrollView.contentOffset = offset;
    }];
    //设置当前页
    self.pageControl.currentPage=currentPage;
}

#pragma mark - ScrollView Delegate
//根据偏移量获取当前页码
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    //获取偏移量
    CGPoint offset=scrollView.contentOffset;
    //计算当前页码
    NSInteger currentPage=offset.x / zScrollViewWidth;
    //设置当前页码
    self.pageControl.currentPage=currentPage;
}

//设置代理方法,当开始拖拽的时候,让计时器停止
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"begin");
    //使定时器失效
    [self.timer invalidate];
    self.timer = nil;
}

//设置代理方法,当拖拽结束的时候,调用计时器,让其继续自动滚动
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    NSLog(@"end");
    //重新启动定时器
    [self loadTimer];
}

@end
